package com.cms.room

import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import androidx.room.migration.Migration
import androidx.sqlite.db.SupportSQLiteDatabase

/**
 * @author: Mr.You
 * @create: 2022-10-10 09:29
 * @description:
 **/
@Database(entities = [Student::class], version = 1, exportSchema = false)
abstract class StudentDataBase: RoomDatabase() {
     abstract fun studentDao():StudentDao

     companion object{
          private  var instance:StudentDataBase? = null
          fun getStudentDataBase(context: Context):StudentDataBase{
              return instance?: synchronized(this){
                   val db= Room.databaseBuilder(context.applicationContext,
                   StudentDataBase::class.java,"student_database")
                        .allowMainThreadQueries()
                       // .fallbackToDestructiveMigration()
                        .addMigrations(MIGRATION_1_2)
                        .build()
                   instance = db
                   db
              }
          }
          private val MIGRATION_1_2 = object :Migration(1,2){
               override fun migrate(database: SupportSQLiteDatabase) {
                    database.execSQL("alter table student_table add column student_height integer")
               }
          }
     }


}